/**
 * 泛型
 * 使用泛型定义一个数组
 */

// T = any泛型any化 不写的话 是unkown
// 一般使用 =  {}
class ArrayList<T = {}> {
  public element: Array<T>;
  constructor() {
    this.element = [];
  }
  public index: number = 0;

  // 往数组中添加元素
  public add(ele: T) {
    //console.log("this.kk * 3:", this.kk * 3);
    this.checkIndex();
    this.element[this.index++] = ele;
  }

  public checkIndex() {
    if (this.index < 0) {
      throw new Error("数组下标不能为零");
    }
  }

  public size() {
    return this.element ? this.element.length : 0;
  }

  // 第二步：根据索引来查询数组中指定元素
  get(index: number): T {
    return this.element[index];
  }
}

type stuType = { stuname: string; age: number; address: string };
let stuOne: stuType = { stuname: "wnagwu", age: 23, address: "beijing" };
let stuTwo: stuType = { stuname: "lisi", age: 39, address: "shanghai" };
let stuThree: stuType = { stuname: "liuqi", age: 31, address: "nanjing" };

//let arrayList3 = new ArrayList<typeof stuOne>();
let arrayList3 = new ArrayList<stuType>();
arrayList3.add(stuOne);
arrayList3.add(stuTwo);
arrayList3.add(stuThree);
let stuobj = arrayList3.get(1);
console.log("stuobj.stuname", stuobj.stuname);

export default ArrayList;
